import scrapy
from ..items import ImgtestItem

class ImgTestSpider(scrapy.Spider):
    name = 'img_test'
    # allowed_domains = ['baidu.com']
    base_url = 'https://www.wxbqb.com/weixin/index_{}.html'
    page = 1
    start_urls = ['https://www.wxbqb.com/weixin/']

    def parse(self, response, **kwargs):
        print(f"第{self.page}正在爬取")
        ps = response.xpath('//p[@class="box-text"]')
        for p in ps:
            item = ImgtestItem()
            item['img_url'] = p.xpath('./a/img/@data-original').get()
            item['img_title'] = p.xpath('./a/img/@alt').get()
            yield item
        if self.page < 3:
            self.page = self.page + 1
            yield scrapy.Request(url=self.base_url.format(self.page),callback=self.parse)